<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    #small-wrapper {
      width: 400px;
      height: 400px;
      border: solid 1px #888;
      position: absolute;
      left: 50px;
      top: 50px;
    }

    #big_wrapper {
      width: 400px;
      height: 400px;
      position: absolute;
      left: 500px;
      top: 50px;
      border: solid 1px #ddd;
      overflow: hidden;
      display: none;
    }

    #move {
      width: 150px;
      height: 150px;
      background-color: rgba(243, 80, 5, 0.2);
      position: absolute;
      left: 0;
      top: 0;
      display: none;
    }

    img {
      position: absolute;
    }
  </style>
</head>

<body>
  <div id="small-wrapper">
    <img width="100%" src="./phone_small.jpg" alt="">
    <div id="move"></div>
  </div>
  <div id="big_wrapper">
    <img id="big_img" src="./phone_big.jpg" alt="">
  </div>

  <script>
    //获取 small-wrapper
    let smallWrapper = document.querySelector('#small-wrapper');
    let bigWrapper = document.querySelector('#big_wrapper');
    let move = document.querySelector('#move');
    let big_img = document.querySelector('#big_img');

    //绑定鼠标移入事件
    smallWrapper.onmouseover = function () {
      //显示移动 div
      move.style.display = 'block'
      //显示大图
      bigWrapper.style.display = 'block';
      //设置鼠标样式
      move.style.cursor = 'move';
      //调用函数
      init();
    }

    //绑定鼠标离开事件
    smallWrapper.onmouseout = function () {
      //隐藏移动 div
      move.style.display = 'none'
      //隐藏大图
      bigWrapper.style.display = 'none';
    }

    //绑定鼠标移动事件
    smallWrapper.onmousemove = function (e) {
      //获取鼠标水平方向的偏移
      let x = e.clientX;
      let y = e.clientY;
      //获取smallWrapper 距离左侧偏移
      let sL = smallWrapper.offsetLeft;
      let sT = smallWrapper.offsetTop;
      //获取 smallWrapper 宽度的一半
      let sW = move.offsetWidth / 2;
      let sH = move.offsetHeight / 2;
      //计算 move 元素的 left 值
      let newLeft = x - sL - sW;
      let newTop = y - sT - sH;
      //计算最大的left值
      let maxLeft = smallWrapper.offsetWidth - move.offsetWidth;
      //进行边界检测
      if (newLeft < 0) {
        newLeft = 0
      }
      if (newLeft > maxLeft) {
        newLeft = maxLeft;
      }
      //计算最大的 top 值
      let maxTop = smallWrapper.offsetHeight - move.offsetHeight;
      if (newTop < 0) {
        newTop = 0;
      }
      if (newTop > maxTop) {
        newTop = maxTop;
      }
      //设置 move 元素的 left 值
      move.style.left = newLeft + 'px';
      move.style.top = newTop + 'px';

      //控制右侧大图的位置
      //计算右侧大图的水平偏移
      let newL = newLeft / smallWrapper.offsetWidth * 800;
      big_img.style.left = -newL + 'px';

      let newT = newTop / smallWrapper.offsetHeight * 800;

      big_img.style.top = -newT + 'px';
    }

    //封装函数设置 move div 的宽度
    function init() {
      //设置 move div 的宽度与高度
      let l = bigWrapper.offsetWidth / 800 * smallWrapper.offsetWidth;
      let h = bigWrapper.offsetHeight / 800 * smallWrapper.offsetHeight;

      //设置 move div 的宽度
      move.style.width = l + 'px';
      move.style.height = h + 'px';
    }
    
  </script>
</body>

</html>